package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.EquipmentInfo;
import com.hushao.lims.weblims.domain.EquipmentRepair;
import com.hushao.lims.weblims.domain.EquipmentRepertory;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.repository.EquipmentRepairsRepository;
import com.hushao.lims.weblims.service.EquipmentRepairService;
import com.hushao.lims.weblims.service.EquipmentRepertoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 930588706
 * @date 18/1/28
 */
@Service
public class EquipmentRepairServiceImpl implements EquipmentRepairService {
    @Autowired
    private EquipmentRepertoryService equipmentRepertoryService;
    @Autowired
    private EquipmentRepairsRepository equipmentRepairsRepository;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentRepair applyRepais(EquipmentInfo equipmentInfo, Integer equipmentCount, TeacherInfo teacherInfo) throws Exception {
        EquipmentRepertory equipmentRepertory=equipmentRepertoryService.findByEquipment(equipmentInfo);
        if(equipmentRepertory.getEquipmentCount()<equipmentCount){
            throw new Exception("当前报修器材数量不得大于其库存数量！");
        }
        EquipmentRepair equipmentRepairs=new EquipmentRepair(equipmentInfo,equipmentCount,null,teacherInfo,null,null,null);
        return  equipmentRepairsRepository.save(equipmentRepairs);
    }

    @Override
    public Page<EquipmentRepair> listEquipmentRepertoryByTeacherAndEquipmentName(String equipmentName, TeacherInfo teacher, Pageable pageable) {
        equipmentName="%"+equipmentName+"%";
        return equipmentRepairsRepository.findByEquipmentInfo_EquipmentNameLikeAndApplyTeacher(equipmentName,teacher,pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(int repairsId) throws Exception {
        EquipmentRepair equipmentRepairs=equipmentRepairsRepository.getOne(repairsId);
        if(equipmentRepairs==null){
            throw  new Exception("此器材报修记录不存在！");
        }else if(!"0".equals(equipmentRepairs.getRepairState())){
            throw new Exception("此器材报修已被审核！");
        }
        equipmentRepairsRepository.delete(equipmentRepairs);
    }

    @Override
    public Page<EquipmentRepair> listEquipmentRepairEquipmentNameLikeAndStateLike(String equipmentName, String state, Pageable pageable) {
        equipmentName="%"+equipmentName+"%";
        state="%"+state+"%";
        return equipmentRepairsRepository.findByEquipmentInfo_EquipmentNameLikeAndAndRepairStateLike(equipmentName,state,pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentRepair audit(int repairId, String state, TeacherInfo teacher) throws Exception {
        EquipmentRepair equipmentRepair=equipmentRepairsRepository.getOne(repairId);
        if(equipmentRepair==null){
            throw  new Exception("此器材报修记录不存在！");
        }
        if(!"0".equals(equipmentRepair.getRepairState())){
            throw new Exception("此器材报修信息已被审核！");
        }
        equipmentRepair.setAuditTeacher(teacher);
        equipmentRepair.setRepairState(state);
        return equipmentRepairsRepository.save(equipmentRepair);
    }

    @Override
    public EquipmentRepair findOne(int repairId) {
        return equipmentRepairsRepository.getOne(repairId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentRepair achieve(int repairId, BigDecimal repairPrice) throws Exception {
        EquipmentRepair equipmentRepair=equipmentRepairsRepository.getOne(repairId);
        if(equipmentRepair==null){
            throw  new Exception("此器材报修记录不存在！");
        }
        if("0".equals(equipmentRepair.getRepairState())){
            throw new Exception("此器材报修信息未被审核！");
        }
        if(!"1".equals(equipmentRepair.getRepairState())){
            throw new Exception("此器材报修信息未被通过！");
        }
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
        equipmentRepair.setAuditTime(dateFormat.format(date));
        equipmentRepair.setRepairState("3");
        equipmentRepair.setRepairPrice(repairPrice);
        return equipmentRepairsRepository.save(equipmentRepair);
    }
}
